import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Article from '../views/Article.vue'
import Register from '../views/Register.vue'
import Login from '../views/Login.vue'
import UserInfo from '../views/UserInfo.vue'
import Edit from '../views/Edit.vue'


Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  }, {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      keepalive: true
    }
  }, {
    path: '/register',
    name: 'register',
    component: Register
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/userinfo',
    name: 'UserInfo',
    component: UserInfo,
    meta: {
      istoken: true
    }
  },
  {
    path: '/edit',
    name: 'Edit',
    component: Edit,
    meta: {
      istoken: true
    }
  },
  {
    path: '/article/:id',
    name: 'Article',
    component: Article,
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if ((!sessionStorage.getItem("id") || !sessionStorage.getItem("token")) && to.meta.istoken == true) {
    router.push('/login');
    Vue.prototype.$msg.fail('请先登录')
    return
  }
  next();
})


export default router
